import QtQuick 1.1
import com.nokia.meego 1.0

PageStackWindow {
    id: appWindow
    property int pageMargin: 16
    initialPage: mainPage

    // the following two properties are set from the C++ code
    property bool iAmActive: false
    property bool levelContinuable: false

    signal sendQMLMessage(int messageID, bool continuePreviousGame)
    signal sendRumbleMessage(int rumbleMessage) // rumbleMessage is ignored actually
    signal gameFinishedQML(int numTrUs, int numTiUs)
    signal musicPlayPauseQML();

    onGameFinishedQML:{
        gameFinished.numTimeUsed = numTiUs
        gameFinished.numTrapsUsed = numTrUs
        pageStack.push(gameFinished)
    }
    showStatusBar: false

    Page {
        id: mainPage
        orientationLock: PageOrientation.LockLandscape
        Image {
            //id: bgImage
            //fillMode: Image.Stretch
            anchors.fill: parent
            source: "qrc:images/mainMenuBG.jpg"
        }

        tools: mainPageTools
        ToolBarLayout {
            id: mainPageTools
            //ToolButton { iconSource: "toolbar-back"; onClicked: Qt.quit()}
            ToolButton { text: "Quit"; onClicked: Qt.quit()}
            ToolButton { text: "About"; onClicked: { appWindow.sendRumbleMessage(1); pageStack.push(helpPage)} }
            ToolButton { text: "New game!"; onClicked: { appWindow.sendRumbleMessage(1); appWindow.sendQMLMessage(1, false)} } // false means start from scratch
            ToolIcon { iconId: "toolbar-mediacontrol-pause"; onClicked: { appWindow.sendRumbleMessage(1); appWindow.musicPlayPauseQML(); } }
        }
        Button{
            id: continueGame
            enabled: appWindow.levelContinuable
            anchors.bottom: mainPage.bottom
            anchors.bottomMargin: 15
            anchors.horizontalCenter: mainPage.horizontalCenter
            text: "Continue from last level"
            onClicked: { appWindow.sendRumbleMessage(1); appWindow.sendQMLMessage(1, true)}
        }

        //////////////////
        Text {
                     id: myText
                     anchors.centerIn: parent
                     color: "white"
                     text: "Trap, Shake, Kill 'em!"
                     //font.pixelSize: 50
                     smooth: true

                     SequentialAnimation on font.letterSpacing {
                         running: appWindow.iAmActive
                         loops: Animation.Infinite;
                         NumberAnimation { from: 0; to: 100; easing.type: Easing.InQuad; duration: 4000 }
                     }

                     SequentialAnimation on font.pixelSize {
                         running: appWindow.iAmActive
                         loops: Animation.Infinite;
                         NumberAnimation { from: 1; to: 50; easing.type: Easing.InQuad; duration: 2000 }
                         PauseAnimation { duration: 2000 }
                     }
                     SequentialAnimation on opacity {
                         running: appWindow.iAmActive
                         loops: Animation.Infinite;
                         NumberAnimation { from: 1; to: 0; duration: 3500 }
                         PauseAnimation { duration: 500 }
                     }
                 }
        //////////////////
    } // end mainPage

    Page {
        id: helpPage
        orientationLock: PageOrientation.LockLandscape
        Image {
            //id: bgImage
            //fillMode: Image.Stretch
            anchors.fill: parent
            source: "qrc:images/mainMenuBG.jpg"
        }
        tools: helpPageTools

        ListModel {
            id: helpList
            ListElement {
                myTopic: "Trap, Shake, Kill 'em"
                myDescription: "By Miika Ahdesmaki (2011). Version 1.1.1. https://projects.developer.nokia.com/MTCatchShake"
            }
            ListElement {
                myTopic: "How to play:"
                myDescription: "Use both thumbs to create a trap on screen.  Catch an enemy in the trap and shake the device for a kill!"
            }
            ListElement {
                myTopic: "Watch out!"
                myDescription: "Don't kill the friendly U.F.O:s though! The circle indicator at the top right corner indicates how much time remains by getting smaller in diameter. Segments of the circle are consumed when deploying traps! Hitting the indicator pauses the game."
            }
            ListElement {
                myTopic: "Images:"
                myDescription: "ESA/NASA http://www.spacetelescope.org/"
            }
            ListElement {
                myTopic: "Music:"
                myDescription: "http://www.dreamstate.to/"
            }
         }
        ListView {
            anchors.centerIn: parent
            width: appWindow.width * 0.8
            height: 350
            model: helpList
            delegate: Text {
                width: parent.width
                wrapMode: Text.WordWrap
                font.pixelSize: 30
                //color: "steelblue"
                //style: Text.Raised; styleColor: "white"
                color: "lightskyblue"
                style: Text.Outline; styleColor: "black"
                text: myTopic + "<br />" + myDescription + "<br />"
            }
         }

        ToolBarLayout {
            id: helpPageTools
            visible: true
            ToolIcon { iconId: "toolbar-back"; onClicked: { pageStack.pop(); } }
            //ToolButton { iconSource: "toolbar-back"; onClicked: { pageStack.pop(); } }
        }
    } // end helpPage

    Page {
        id: gameFinished
        property int numTrapsUsed: 0
        property int numTimeUsed: 0
        orientationLock: PageOrientation.LockLandscape
        Image {
            //id: bgImage
            //fillMode: Image.Stretch
            anchors.fill: parent
            source: "qrc:images/gameFinishedBG.jpg"
        }
        tools: finishedPageTools

        Text{
            id: scoreText
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.bottom: parent.bottom
            font.pixelSize: 36
            style: Text.Outline; styleColor: "red"
            color: "crimson"
            text: "Traps deployed: " + gameFinished.numTrapsUsed + ". Time used: " + gameFinished.numTimeUsed
        }

        ListModel {
            id: finishedList
            ListElement {
                myTopic: "Congratulations!"
                myDescription: "You have just finished all levels. Well done and thanks for playing!"
            }
            ListElement {
                myTopic: "Credits:"
                myDescription: "All code by Miika Ahdesmaki 2011. Feedback welcome: miika.ahdesmaki@ovi.com"
            }
         }
        ListView {
            anchors.centerIn: parent
            width: appWindow.width * 0.8
            height: 350
            model: finishedList
            delegate: Text {
                width: parent.width
                wrapMode: Text.WordWrap
                font.pixelSize: 30
                //color: "steelblue"
                //style: Text.Raised; styleColor: "white"
                color: "lightskyblue"
                style: Text.Outline; styleColor: "darkblue"
                text: myTopic + "<br />" + myDescription + "<br />"
            }
         }
        ToolBarLayout {
            id: finishedPageTools
            visible: true
            ToolIcon { iconId: "toolbar-back"; onClicked: { pageStack.pop(); } }
            //ToolButton { iconSource: "toolbar-back"; onClicked: { pageStack.pop(); } }
        }
    } // end gameFinished
}
